<!--
 * @description  :涉案人信息列表
 * @descriptionDetail:涉案人信息列表
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
-->
<template>
  <view>
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="nolicence-search-lay" :style="{'top': isHideNav ? 0 : '44px'}">
      <u-search
        shape="round"
        :clearabled="true"
        :showAction="true"
        actionText="确定"
        :animation="true"
        placeholder="姓名/电话/现地址"
        v-model="searchKey"
        @custom="toSearchList"
      ></u-search>
    </view>
    <view class="content u-page">
      <me-table
        :i="0"
        :index="0"
        :top="114"
        :load-data="loadData"
        :height="getListViewHeight()"
        :init-load="false"
        ref="mescrollItem0"
      >
        <view slot-scope="d" class="list-item-root">
          <u-swipe-action
            :show="d.data.show"
            :index="d.data.uuid"
            :disabled="d.data.swiperDisabled"
            @click="swiperClick"
            @open="swiperOpen"
            :options="d.data.swiperOptions"
          >
            <view class="list-lay">
              <list-item-person-involved :itemInfo="d.data"></list-item-person-involved>
            </view>
          </u-swipe-action>
        </view>
      </me-table>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button class="u-btn" type="primary" @click="addPersonInvolved">
        添加
      </u-button>
    </view>
  </view>
</template>

<script>
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import meTable from "@/components/me-table.vue";
import listItemPersonInvolved from "./components/listItemPersonInvolved.vue";
import intelligenceservice from "@/service/intelligence/intelligence.service";
export default {
  components: {
    meTable,
    listItemPersonInvolved,
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      searchKey:"",
      tagShow: false,
      customStyle: {
        "margin-left": "5px",
        "margin-right": "5px",
        "padding-left": "15px",
        "padding-right": "15px",
        "margin-bottom": "10px",
      },
      popupHeight:"500rpx",
      tagList:[{
        name:'零售户',
        code:'retailer',
        isChoice:false,
      },{
        name:'案件',
        code:'cases',
        isChoice:false,
      },{
        name:'车辆',
        code:'cars',
        isChoice:false,
      },
      {
        name:'人员',
        code:'persons',
        isChoice:false,
      },
      {
        name:'天天',
        code:'persons1',
        isChoice:false,
      },
      {
        name:'钉钉',
        code:'persons2',
        isChoice:false,
      },
      ],
      tmpSelectTags:"",
      selectTags:"",
      user: this.$storage.get(this.$rp.common.USER),
      defaultOrg:undefined,
      orgName:"",
      orgUuid:"",
    }
  },
  methods: {
    getListViewHeight() {
      let p = (this.$u.sys().windowHeight - 44 - 48 - uni.upx2px(10) - uni.upx2px(150) - this.$u.sys().statusBarHeight) + "px";
      // #ifdef H5-DINGDING || H5-WECHAT
      p = (this.$u.sys().windowHeight - uni.upx2px(10) - 48 - uni.upx2px(150) - this.$u.sys().statusBarHeight) + "px";
      // #endif
      return p;
    },
    getItemCanEdit(item){
      let disabled = true;
      let swiperOptions = [];
      if(item.ableOperate == 1){
        disabled = false;
        swiperOptions.push({
          text: '编辑',
          action:"edit",
          style: {
            backgroundColor: this.$theme.u_type_primary,
          },
        });
      }
      if(item.isActive == 1){
        swiperOptions.push({
          text: '停用',
          action:"deactivate",
          style: {
            backgroundColor: this.$theme.u_type_error,
          },
        });
      } else {
        swiperOptions.push({
          text: '启用',
          action:"enable",
          style: {
            backgroundColor: this.$theme.u_type_success,
          },
        });
      }
      item.swiperOptions = swiperOptions;
      item.swiperDisabled = disabled;
    },
    toSearchList(e){
      this.getMescroll(0).triggerDownScroll();
    },
    tagResultListener(arr){
      let dataCode = [];
      if (arr.length > 0) {
        arr.forEach(item => {
          dataCode.push(item.code);
        });
      }
      this.tmpSelectTags = dataCode.join(",");
    },
    confirmTag(){
      this.selectTags = this.tmpSelectTags;
      this.tagShow = false;
      this.getMescroll(0).triggerDownScroll();
    },
    resetTag(){
      this.$refs.choiceTag.resetState();
      this.tmpSelectTags = "";
      this.selectTags = "";
      this.tagShow = false;
      this.getMescroll(0).triggerDownScroll();
    },
    tagPopupClose(){
      let selectTagArr = this.selectTags.split(",");
      this.tagList.map((itm,idx) => {
        itm.isChoice = selectTagArr.includes(itm.code);
      });
    },
    loadData(page) {
      return new Promise((resolve,reject) => {
        let params = {
          page:page.num,
          limit:page.size,
          paras:{
            fuzzyQuery:this.searchKey,
          },
        }
        if (this.defaultOrg) {
          params.paras.orgUuid = this.defaultOrg.id;
          intelligenceservice.personInfoPage(params).then(res => {
            if(res.success){
              res.data.forEach(itm => {
                this.getItemCanEdit(itm);
                itm.show = false;
                itm.swiperDisabled = false;
              });
              resolve({
                count: res.count,
                data:res.data,
              });
            }else{
              this.$u.toast(res.msg);
              resolve({
                count: 0,
                data:[],
              });
            }
          }).catch(e => {
            this.$u.toast('请求失败:' + e);
            resolve({
              count: 0,
              data:[],
            });
          });
        } else {
          this.getDefaultOrg("", "zm-all", (r) => {
            if (r) {
              this.defaultOrg = r;
              this.orgName = r.name;
              this.orgUuid = r.id;
              params.paras.orgUuid = this.defaultOrg.id;
            } else {
              params.paras.orgUuid = "";
            }
            intelligenceservice.personInfoPage(params).then(res => {
              if(res.success){
                res.data.forEach(itm => {
                  this.getItemCanEdit(itm);
                  itm.show = false;
                  itm.swiperDisabled = false;
                });
                resolve({
                  count: res.count,
                  data:res.data,
                });
              }else{
                this.$u.toast(res.msg);
                resolve({
                  count: 0,
                  data:[],
                });
              }
            }).catch(e => {
              this.$u.toast('请求失败:' + e);
              resolve({
                count: 0,
                data:[],
              });
            });
          });
        }
      })
    },
    swiperBtn (id,isActive) {
      if (isActive == 1) {
        this.$modal.confirm("确定要停用吗？", () => {
          this.$modal.showLoading("停用中...");
          let params = { "uuid": id, "type": "2", };
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("停用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("停用失败" + err);
          })
        });
      } else {
        this.$modal.confirm("确定要启用吗？", () => {
          this.$modal.showLoading("启用中...");
          let params = {"uuid":id,"type":"2",};
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("启用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("启用失败" + err);
          })
        });
      }
    },
    // index1是数据索引 index是导航索引
    swiperClick(index1, index){
      let datas = this.$refs.mescrollItem0.listDatas;
      let cur = -1;
      for (let i = 0; i < datas.length; i++) {
        if(datas[i].uuid == index1){
          cur = i;
          break;
        }
      }
      if(cur == -1){
        this.$u.toast("选中数据无效");
        return;
      }
      if (index == 0) { // 修改
        if (datas[cur].ableOperate == 1) {
          let params = datas[cur];
          this.setRouteParam(params);
          this.$u.route({
            url: "/pages/intelligence/intelligenceFilesAdd/personsInvolved",
            params: {
              from: "edit",
            },
          });
          this.$refs.mescrollItem0.listDatas.map((val, idx) => {
            this.$refs.mescrollItem0.listDatas[idx].show = false;
          });
          return;
        } else {
          this.swiperBtn(datas[cur].uuid,datas[cur].isActive);
        }
      }else if(index == 1){ // 删除
        this.swiperBtn(datas[cur].uuid,datas[cur].isActive);
      }
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = false;
      });
    },
    addPersonInvolved(){
      this.$u.route({
        url:"pages/intelligence/intelligenceFilesAdd/personsInvolved",
        params: {
          orgName: this.orgName,
          orgUuid: this.orgUuid,
        },
      });
    },
    updateBackState(param){
      this.getMescroll(0).triggerDownScroll();
    },
    swiperOpen(index){
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = index == val.uuid;
      });
    },
  },
}
</script>

<style scoped lang="scss">
	@import "@/style/common/rightMenu.scss";
	.content {
  background-color: #f3f4f5;
    padding-bottom:160rpx;
	}
  .nolicence-search-lay {
    background-color: #fff;
    position: fixed;
    // #ifndef H5-DINGDING
    top: 44px;
    // #endif
    // #ifdef H5-DINGDING
    top: 0px;
    // #endif
    z-index: 999;
    width: 100%;
    overflow: hidden;
    padding: $cm-search-padding-row $cm-search-padding-column;
  }
	.key-search{
		width: 100%;
		margin: auto;
		padding-left: 2.5%;
		padding-right: 2.5%;
		line-height: 48px;
	}
	.pop-content-lay{
		padding: 20rpx;
		display: flex;
		height: 100%;
		flex-direction: column;
		justify-content: flex-start;
	}
	.list-item-root{
  margin: 10px;
  border-radius: 15px;
  overflow: hidden;
	}
	.list-lay{
		margin: 10px;
	}
	.tag-lay-btns{
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		width: 100%;
		uni-button{
			flex: 0.4;
		}
	}
</style>
